/*	kd:
	
	I like it when animations are smooth. This shop is no exception.
	
	This will maintain caches and a timer in such a way that you can draw an
	animation that interpolates your previous and current inventory state, so
	lamps will gradually light up as you gain items.
	
	*/

struct Gc_Shop_CacheSmoothing play {
	
	const delay_tics			= 2;
	const default_fade_tics		= 14;
	const default_update_tics	= 8 * Thinker.ticrate;
	
	// kd: Deliberately not private. The first two caches are what is shown.
	// When the animation is done, fading_to_cache overwrites cache, and
	// buffer_cache overwrites fading_to_cache. Then the game starts over.
	
	// In the meantime, all your inventory changes are recorded in buffer_cache.
	Gc_Shop_DrawCache				cache;
	Gc_Shop_DrawCache				fading_to_cache;
	// Gc_Shop_DrawCache				buffer_cache;
	FadeTick						fade_t;
	private FadeTick				delay_t;
	private int						fade_tics_;
	private bool					force_update;
	
	// private bool					is_updating;
	private bool					has_buffer_cache;
	
	void ClearAndResize (int size) {
		cache.ClearAndResize(size);
		fading_to_cache.Copy(cache);
		// buffer_cache.Copy(cache);
	}
	
	void ResetFade (int fade_tics = default_fade_tics) {
		fade_tics_ = max(1, fade_tics);
		force_update = false;
	}
	
	// kd: This effectively causes a jump in animation, so for me, it's only
	// useful when the shop is just loaded.
	void ForceTo (
	PlayerInfo			player,
	Gc_Shop_Inv			inv,
	Gcs_ButtonSprites	sprites) {
		cache.FillFromInventory(player, inv, sprites);
		fading_to_cache.Copy(cache);
		// buffer_cache.Copy(cache);
	}
	
	// kd: And this is what you'd call to cause a gradual change.
	void To (
	PlayerInfo			player,
	Gc_Shop_Inv			inv,
	Gcs_ButtonSprites	sprites) {
		has_buffer_cache = true;
		
		// kd: There was no recent update, so we'll do one now. It has to wait a
		// few tics to ensure the net events went through (so we get the
		// player's latest inventory).
		
		if(delay_t.IsDone() && fade_t.Now() < 1) {
			delay_t.To(delay_tics, from: 0);
		}
	}
	
	void NextState (
	PlayerInfo			player,
	Gc_Shop_Inv			inv,
	Gcs_ButtonSprites	sprites) {
		let delay_just_done = delay_t.IsDone();
		delay_t.Next();
		delay_just_done = !delay_just_done && delay_t.IsDone();
		
		if(delay_just_done) {
			// buffer_cache.FillFromInventory(player, inv, sprites);
			fading_to_cache.FillFromInventory(player, inv, sprites);
			// fading_to_cache.Copy(buffer_cache);
			fade_t.To(fade_tics_, from: 0);
			delay_t.ForceDone();
			has_buffer_cache = false;
		}
		
		fade_t.Next();
		
		if(fade_t.IsDone() && 0 < fade_t.Now()) {
			fade_t.Force(0);
			cache.Copy(fading_to_cache);
			
			// kd: Apparently we got another request while fading, so do it
			// all over again.
			if(has_buffer_cache) {
				delay_t.To(delay_tics, from: 0);
			}
		}
	}
	
	int FadeTics () const {
		return fade_tics_;
	}
	
	// kd: Additionally, it can update every few seconds.
	private FadeTick				update_t;
	private int						auto_update_tics;
	private bool					can_auto_update;
	
	void ResetAutoUpdate (int update_tics = default_update_tics) {
		auto_update_tics = max(fade_tics_, update_tics);
		update_t.To(auto_update_tics);
		force_update = false;
	}
	
	void AllowAutoUpdate (bool can_auto_update_) {
		can_auto_update = can_auto_update_;
	}
	
	void NextAutoUpdate () {
		if(!can_auto_update) {
			return;
		}
		
		update_t.Next();
	}
	
	bool WantsAutoUpdate () const {
		return can_auto_update && update_t.IsDone();
	}
}
